<?php

namespace App\Models;

use App\Models\Order\Order;
use Hyn\Tenancy\Traits\UsesTenantConnection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Customer
 *
 * @property int $id
 * @property string $name
 * @property string|null $address
 * @property string|null $tel
 * @property string|null $email
 * @property string|null $contact_name
 * @property string|null $contact_tel
 * @property string|null $contact_email
 * @property string|null $identify_code 身份证号（个⼈）/开票信息（企业）
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Order\Order[] $orders
 * @property-read int|null $orders_count
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer fuzzyFilter($queryString)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Customer onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereContactEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereContactName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereContactTel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereIdentifyCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereTel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Customer whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Customer withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Customer withoutTrashed()
 * @mixin \Eloquent
 */
class Customer extends Model
{
    use SoftDeletes, UsesTenantConnection;
    protected $fillable = ['name', "address", 'tel', 'email', 'contact_name', 'contact_tel', 'contact_email', 'identify_code'];

    public function orders()
    {
        return $this->hasMany(Order::class, 'customer_id');
    }

    public function scopeFuzzyFilter($query, $queryString)
    {
        $likeStr = '%' . $queryString . '%';

        $query->where('name', 'like', $likeStr)
            ->orWhere('address', 'like', $likeStr)
            ->orWhere('tel', 'like', $likeStr)
            ->orWhere('email', 'like', $likeStr)
            ->orWhere('contact_name', 'like', $likeStr)
            ->orWhere('contact_tel', 'like', $likeStr)
            ->orWhere('contact_email', 'like', $likeStr)
            ->orWhere('identify_code', 'like', $likeStr);
        return $query;
    }
}
